package com.longshine.util;

/**
 * 数字 进制转换
 * @author Administrator
 *
 */
public class NumberUtil {
	/**
	 * 2进制转十进制
	 * @param s
	 * @return
	 */
	public static long binary2Decimal(String s){
		char[] cs = s.toCharArray();
		long ret = 0;
		for(int i=0;i<cs.length;i++){
			ret += Math.pow(2, cs.length-i-1)*(cs[i]=='1'?1:0); 
		}
		return ret;
	}
	
	public static String decimal2Binary(int decNum){
		String ret = "";
		while(decNum>0){
			int dev = decNum /2;
			int lef = decNum %2;
			ret = lef + ret;
			decNum = dev;
		}
		return ret;
	}
	/**
	 * 十六进制转2进制
	 * @param hex
	 * @return
	 */
	public static String hex2Binary(String hex){
		int l = hex.length();
		String ret= "";
		for(int i = 0;i<l;i++){
			ret += formatBinaryStr(hexOneStr2Binary(hex.charAt(i)+""),4);
		}
		return ret;
	}
	/**
	 * 16进制的4位（1/2）字节转化为二进制 如F --1111
	 * @param hex
	 * @return
	 */
	private static String hexOneStr2Binary(String hex){
		return decimal2Binary(hex2Decimal(hex));
	}
	/**
	 * 格式化二进制
	 * @param bStr
	 * @param len
	 * @return
	 */
	public static String formatBinaryStr(String bStr,int len){
		int l = bStr.length();
		if(l == len){
			return bStr;
		}
		int less = len - l;
		for(int i = 0; i< less; i++){
			bStr = "0" +  bStr;
		}
		return bStr;
	}
	/**
	 * 二进制转十六进制
	 * @param binaStr
	 * @param num
	 * @return
	 */
	public static String binary2Hex(String binaStr,int num){
		return toHexStr(binary2Decimal(binaStr),num);
	}
	/**
	 * 将16进制字符串转为10进制
	 * @param num
	 * @return
	 */
	public static int hex2Decimal(String num){
		int ret = 0;
		num = num.toLowerCase();
		char[] chars = num.toCharArray();
		for(int i =0;i<chars.length;i++){
			char c = chars[i];
			ret = ret<<4;
			ret += (c>='a'?(10+c-'a'):(c-'0'));
		}
		return ret;
	}
	/**
	 * 十进制转 十六进制
	 * @param num 0-255
	 * @return
	 */
	public static String toHexStr(long num){
		long t = num;
		String ret = "";
		long devidedNum = t / 16;
		long leftNum = t % 16;
		ret += toHexChar(devidedNum);
		ret += toHexChar(leftNum);
		return ret;
	}
	
	/**
	 * 十进制转十六进制
	 * @param num
	 * @param byteNum
	 * @return
	 */
	public static String toHexStr(long num,int byteNum){
		String ret = "";
		long t = num;
		while(byteNum >0){
			long dev = t / 256;
			long lef = t % 256;
			ret = toHexStr(lef) + ret;
			t = dev;
			byteNum--;
		}
		return ret;
	}
	
	/**
	 * 将少于16的数转化为十六进制字符
	 * @param num <16
	 * @return
	 */
	public static char toHexChar(long num){
		char ret = '0';
		if(num<10){
			ret = (char)('0'+num);
		}
		else{
			ret =(char)('a'+(num-10));
		}
		return ret;
	}
	
	
	public static String toHexStr(byte[] buf,long l){
		String ret = "";
		for(int i = 0;i <l;i++){
			ret += toHexStr(byte2Int(buf[i]));
		}
		return ret;
	}
	
	public static int byte2Int(byte b){
		if(b<0){
			return 256 + b;
		}
		return b;
	}
	
	public static float hex2Float(String hex){
		String bin = NumberUtil.formatBinaryStr(NumberUtil.hex2Binary(hex),4*8);
		int bSign = Integer.parseInt(bin.substring(0,1));
		String exponent = bin.substring(1,1+8);
		String data = bin.substring(9);
		long exp = NumberUtil.binary2Decimal(exponent)-127;
		data = data.substring(0,data.lastIndexOf('1')+1);
		int len = data.length();
		float ret = 0;
		for(int i = 0; i < len; i ++){
			char c = data.charAt(i);
			if(c=='1'){
				float t = (float)Math.pow(2, -i-1);
				ret = ret + t;
			}
		}
		return (bSign==0?1:-1)*ret*(float)Math.pow(2, exp);
	}
	
	public static String float2Hex(float f){
		String bin = float2Binary(f);
		int dIndex = bin.indexOf(".");
		bin = bin.replaceAll("[.]", "");
		int l = bin.length();
		int sign = f < 0?1:0;
		if(bin.length()>23){
			bin = bin.substring(0,23);
		}
		for (int i = 0;i<23-l;i++){
			bin = bin + "0";
		}
		dIndex += 127;
		bin = sign + NumberUtil.formatBinaryStr(NumberUtil.decimal2Binary(dIndex),8) + bin;
		return NumberUtil.binary2Hex(bin, 4);
	}
	
	public static String float2Binary(float f){
		String head = long2Binary((int)f);
		String t = (f-(int)f)+"";
		int d = t.indexOf(".");
		if(d>0){
			t = t.substring(d + 1);
		}
		long l = (long)((f-(int)f)*Math.pow(2, 23));
		String tail = long2Binary(l);
		tail = NumberUtil.formatBinaryStr(tail, 23);
		head = head +"." + tail;
		String ret = head.substring(0,head.lastIndexOf("1")+1);
		return ret;
	}
	
	public static String long2Binary(long l){
		String ret = "";
		while(l>0){
			long dev = l /2;
			long lef = l %2;
			ret = lef + ret;
			l = dev;
		}
		return ret;
	}

	
	public static byte[] str2ByteArr(String str){
		byte[] retbyte = new byte[str.length()/2];
		String ret = "";
		int i = 0;
		while(str != null&& !"".equals(str)){
			ret= str.substring(0,2);
			str = str.substring(2);
			retbyte[i++] = (byte)CommonUtil.toHex(ret);
		}
		return retbyte;
	}
	public static void main(String[] args){
/*		String a = float2Hex(0.3f);
		System.out.println(a);
		System.out.println(hex2Float(a));*/
//		/6821431F00010081140401 F2E7D443 3FAC0831 01000000 01000000 01000000 01000000 16
//		System.out.println(hex2Float("F2E7D443"));
//		System.out.println(hex2Float("3FAC0831"));
//		System.out.println(hex2Float(StrTmpUtil.tcpStrExchangeSeqence("3FAC0831")));
//		System.out.println(StrTmpUtil.tcpStrExchangeSeqence("F2E7D443"));
		//0 1000 0111 10101001110011111110010
//		System.out.println(hex2Float("43d4e7f2"));
//		System.out.println(hex2Float("43d4e7f2"));
	//	System.out.println(float2Hex(1.25f));
	//	System.out.println(float2Hex(0.25f));
//		System.out.println(hex2Float("40500000"));
//		System.out.println(float2Binary(1.25f));
//		System.out.println(hex2Float("3f400000"));
		//1000 0101 1000 0010 0010 1100 1001 0111
//		System.out.println(NumberUtil.binary2Hex("01000010110000010001011001001011", 4));
		System.out.println(hex2Float(float2Hex(10032.5435443f)));
	}
}
